'''
Created on Apr 29, 2009

@author: caesar
'''

import unittest
import time
import telnetlib

from fetcher import *


class StorageMock(object):
  def __init__(self):
    last_time = time.mktime(time.localtime()) - 300;
    
    self.data = {('mock_module1', 'mock_value_11'): (last_time, 123),\
                 ('mock_module1', 'mock_value_12'): (last_time, 234),\
                 ('mock_module2', 'mock_value_21'): (last_time, 345)}
  def get_handler(self, module, property):
    return StorageHandler(module, property, self.data)
  
  
class TelnetMock (object):
  def __init__(self):
    self.data = ""
    self.supported_commands = {'list\n': 'mock_module1 mock_module2 mock_module3\n',\
                          'config mock_module1\n': """  graph_info mock_module1 graph_info string
 graph_total mock_module1 graph_total string
   graph_title mock_module1 graph_title string
 graph_period minute
   #comment somewhy
graph_vlabel parrots
graph_category NotOther
mock_value_11.label     value_11 label
 mock_value_11.info  value_11 info
    mock_value_11.extinfo   value_11 extinfo
mock_value_11.max    100
  mock_value_11.min   0 
mock_value_11.warning  10 
 mock_value_11.critical    50 

#mock_value_11.label commented label

mock_value_12.label value_12 label
mock_value_12.info value_12 info
mock_value_12.extinfo value_12 extinfo
mock_value_12.max 200 
mock_value_12.min 0 
mock_value_12.warning 20 
mock_value_12.critical 100 
.
""",\
                          'config mock_module2\n': """
                          
graph_title mock_module2 graph_title string
   #comment somewhy
graph_vlabel parrots
mock_value_21.label value_21 label
.
""",\
                          'config mock_module3\n': """
#completely broken module config                          
.
""",\
                          'fetch mock_module1\n': """
 mock_value_11.value   123
mock_value_11.garbage 432                          
mock_value_12.value U                          
.
""",\
                          'fetch mock_module2\n': """

mock_value_21.value   567
wrong_property.value 321
.
""",\
                          'fetch mock_module3\n': """
mock_value_31.value   568
.
""",}
    
    
  def write(self, command):
    try:
      self.data += self.supported_commands[command]
    except KeyError:
      self.data += "# Unknown command. Try list, nodes, config, fetch, version or quit\n" 

  def read_until (self, termination):
    head, sep, self.data = self.data.partition (termination)
    return head + sep

    
  

class TestFetcher (unittest.TestCase):
  def testInit(self):
    tn = TelnetMock()
    storage = StorageMock()
    fetcher = MuninFetcher(tn, storage, True)
    node_state = fetcher.get_node_state()
    for module, module_state in node_state.states.iteritems():
      print "%s :: %s\n" % (module, module_state)
    
if __name__ == "__main__":
  unittest.main()

    